package com.wanpinghui.supplier.business;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.SharedElementCallback;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.business.view.WVideoView;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.common.util.DensityUtil;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 */
public class VideoPlayActivity extends AppCompatActivity {

    private WVideoView videoView;
    private ImageView imageView;
    private String url;
    private int videoHeight;
    private int videoWidth;
    private int screenWidth;
    private int screenHeight;
    private String imgUrl;
    private RelativeLayout relativeLayout;
    private RelativeLayout contentLayout;
    private ProgressBar progressBar;

    public static void startWithElement(Activity context, String url, View view, String thumb, int width, int height) {
        Intent intent = new Intent(context, VideoPlayActivity.class);
        intent.putExtra("url", url);
        if (TextUtil.isNotEmpty(thumb)) {
            intent.putExtra("img_url", thumb);
            intent.putExtra("width", width);
            intent.putExtra("height", height);
        }
        ActivityOptionsCompat compat = null;
        if (view == null) {
            compat = ActivityOptionsCompat.makeSceneTransitionAnimation(context);
        } else {
            compat = ActivityOptionsCompat.makeSceneTransitionAnimation(context, view,
                    "tansition_view");
        }
        ActivityCompat.startActivity(context, intent, compat.toBundle());
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_video_play);

        initView();
        MediaController controller = new MediaController(this);
        controller.setVisibility(View.GONE);
        videoView.setMediaController(controller);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Observable.timer(500, TimeUnit.MILLISECONDS, Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<Long>() {
                            @Override
                            public void call(Long aLong) {
                                imageView.setVisibility(View.GONE);
                                progressBar.setVisibility(View.GONE);
                            }
                        });
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.start();
                mp.setLooping(true);
            }
        });

        setEnterSharedElementCallback(new SharedElementCallback() {

            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                sharedElements.clear();
                sharedElements.put("tansition_view", imageView);
            }
        });
    }

    public void initView() {
        screenWidth = DensityUtil.getScreenWidth();
        screenHeight = DensityUtil.getScreenHeight();
        url = getIntent().getStringExtra("url");
        imgUrl = getIntent().getStringExtra("img_url");
        imageView = (ImageView) findViewById(R.id.image_view);
        videoView = (WVideoView) findViewById(R.id.video_view);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        relativeLayout = (RelativeLayout) findViewById(R.id.rl_area);
        contentLayout = (RelativeLayout) findViewById(R.id.content_layout);
        videoWidth = getIntent().getIntExtra("width",0);
        videoHeight = getIntent().getIntExtra("height",0);
        if (videoWidth > 0 && videoHeight > 0) {
            if (videoWidth / (float) videoHeight > 1) {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                layoutParams.width = screenWidth;
                layoutParams.height = (int) (videoHeight * ((float) screenWidth) / videoWidth);
                imageView.setLayoutParams(layoutParams);

                RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
                layoutParams2.width = screenWidth;
                layoutParams.height = (int) (videoHeight * ((float) screenWidth) / videoWidth);
                videoView.setLayoutParams(layoutParams2);
            } else {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                layoutParams.height = screenHeight;
                layoutParams.width = screenWidth;
                imageView.setLayoutParams(layoutParams);

                RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
                layoutParams2.height = screenHeight;
                layoutParams.width = screenWidth;
                videoView.setLayoutParams(layoutParams2);
            }
        }
        if (TextUtil.isNotEmpty(imgUrl)) {
            Glide.with(this).load(imgUrl).into(imageView);
        }
        videoView.setVideoPath(url);

        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onBackPressed();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.stopPlayback();
    }

    @Override
    protected void onResume() {
        super.onResume();
        contentLayout.setSystemUiVisibility(View.INVISIBLE);
        videoView.start();
    }


    @Override
    public void onBackPressed() {
        relativeLayout.setBackgroundColor(Color.TRANSPARENT);
        imageView.setVisibility(View.VISIBLE);
        videoView.setVisibility(View.GONE);
        contentLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        finishActivity();
//        ObjectAnimator am = ObjectAnimator.ofFloat(imageView,"scaleX",1,0.4f);
//        ObjectAnimator am2 = ObjectAnimator.ofFloat(imageView,"scaleY",1,0.4f);
//        AnimatorSet set = new AnimatorSet().setDuration(500);
//        set.addListener(new Animator.AnimatorListener() {
//            @Override
//            public void onAnimationStart(Animator animation) {
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                finishActivity();
//            }
//
//            @Override
//            public void onAnimationCancel(Animator animation) {
//
//            }
//
//            @Override
//            public void onAnimationRepeat(Animator animation) {
//
//            }
//        });
//        set.play(am).with(am2);
//        set.start();
    }

    private void finishActivity() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition();
        } else {
            finish();
            overridePendingTransition(0,0);
        }
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }

}
